package com.zzyl.nursing.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.constant.HttpStatus;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingProjectMapper;
import com.zzyl.nursing.domain.NursingProject;
import com.zzyl.nursing.service.INursingProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
/**
 * 护理项目Service业务层处理
 * 
 * @author ytr
 * @date 2025-04-16
 */
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService
{
    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    private static final String KEY = "nursingProject:all";

    /**
     * 查询护理项目
     * 
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询护理项目列表
     * 
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject)
    {
        return nursingProjectMapper.selectNursingProjectList(nursingProject);
    }

    private void deleteCache() {
        //删除缓存
        redisTemplate.delete(KEY);
    }

    /**
     * 新增护理项目
     * 
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject)
    {
//        nursingProject.setCreateTime(DateUtils.getNowDate());

        int i = save(nursingProject) ? 1 : 0;
        if (i > 0) {
            deleteCache();
        }
        return i;
    }

    /**
     * 修改护理项目
     * 
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject)
    {
//        nursingProject.setUpdateTime(DateUtils.getNowDate());
        int i = updateById(nursingProject) ? 1 : 0;
        if (i > 0) {
            deleteCache();
        }
        return i;
    }

    /**
     * 批量删除护理项目
     * 
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectByIds(Long[] ids)
    {
        int i = removeByIds(Arrays.asList(ids)) ? 1 : 0;
        if (i > 0) {
            deleteCache();
        }
        return i;
    }

    /**
     * 删除护理项目信息
     * 
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectById(Long id)
    {
        int i = removeById(id) ? 1 : 0;
        if (i > 0) {
            deleteCache();
        }
        return i;
    }

    /**
     * 查询所有护理项目
     *
     * @return
     */
    @Override
    public List<NursingProjectVo> getNursingProjectList() {
        return nursingProjectMapper.getNursingProjectList();
    }

    /**
     * 查询所有护理项目
     *
     * @return
     */
    @Override
    public List<NursingProjectVo> selectAll() {
        //1.从redis中获取数据
        List<NursingProjectVo> list = (List<NursingProjectVo>) redisTemplate.opsForValue().get(KEY);
        //2.判断缓存中是否存在数据，如果redis中存在数据，直接返回
        if(list != null && list.size() > 0){
            return list;
        }
        //3.如果redis中不存在数据，从数据库中查询，并把数据保存到redis中，然后返回
        list = nursingProjectMapper.selectAll();
        redisTemplate.opsForValue().set(KEY,list);
        return list;
    }

    /**
     * 分页查询护理项目列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public TableDataInfo selectPage(Integer pageNum, Integer pageSize) {
        // 分页查询数据
        IPage page = new Page(pageNum, pageSize);

        page = page(page,null);
        return getTableDataInfo(page);
    }

    /**
     * 封装分页数据
     * @param page
     * @return
     */
    private TableDataInfo getTableDataInfo(IPage page) {
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("请求成功");
        tableDataInfo.setTotal(page.getTotal());
        tableDataInfo.setRows(page.getRecords());
        return tableDataInfo;

    }
}
